package org.totoro.patterns.behavior.interpreter.demo;

import java.util.List;

/**
 * @author YHL
 * @version V1.0
 * @Description: 多条件
 * @date 2018-11-20
 */
public class CompoundPredicate extends PredicateAbs<Boolean> {

    public enum Logic {
        /**
         * 并且
         **/
        AND,

        /**
         * 或者
         **/
        OR
    }

    private Logic logic = Logic.AND;

    private List<IExpression<Boolean>> list;

    public CompoundPredicate(Logic logic, List<IExpression<Boolean>> list) {
        this.logic = logic;
        this.list = list;
    }

    @Override
    public Boolean evaluate(Object context) {

        return true;
    }

    public <V> CompoundPredicate add(V v, IExpression expression) {

        list.add(Expressions.eq(v, expression));

        return this;
    }

    public <V> CompoundPredicate add(IExpression expression) {

        list.add(expression);

        return this;
    }


    public <V> CompoundPredicate and(IExpression expression) {

        CompoundPredicate compoundPredicate = Expressions.and(expression);

        return compoundPredicate;
    }

    @Override
    public String toString() {

        StringBuffer sb = new StringBuffer();

        String logicName = logic.name() + " ";

        sb.append("(");

        list.forEach(e -> {
            sb.append(e.toString() + " " + logicName);
        });

        sb.delete(sb.length() - logicName.length(), sb.length());

        sb.append(")");

        return sb.toString();
    }
}
